//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "DepositRateHelper.h"
using namespace Cephei::QL::Termstructures::Yield;
#include <gen/QL/Indexes/IborIndex.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Termstructures/Yield/RateHelper.h>
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL;
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Yield::CDepositRateHelper::CDepositRateHelper (Double rate, Cephei::QL::Indexes::IIborIndex^ iborIndex) : CRateHelper(CDepositRateHelper::typeid)
{
    CIborIndex^ _CiborIndex;
    try
    {
#ifdef HANDLE
        _phDepositRateHelper = NULL;
#endif
        QuantLib::Rate _rate = (QuantLib::Rate)ValueHelper::Convert (rate); //d
        _CiborIndex = safe_cast<CIborIndex^> (iborIndex);
        _CiborIndex->Lock();
        boost::shared_ptr<QuantLib::IborIndex>& _iborIndex = static_cast<boost::shared_ptr<QuantLib::IborIndex>&> (_CiborIndex->GetShared ()); 
        _ppDepositRateHelper = new boost::shared_ptr<QuantLib::DepositRateHelper> (new QuantLib::DepositRateHelper ( _rate,  _iborIndex ));
        SetRateHelper (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppDepositRateHelper));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CiborIndex != nullptr) _CiborIndex->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::CDepositRateHelper::CDepositRateHelper (Cephei::QL::IQuote^ rate, Cephei::QL::Indexes::IIborIndex^ iborIndex) : CRateHelper(CDepositRateHelper::typeid)
{
    CQuote^ _Crate;
    CIborIndex^ _CiborIndex;
    try
    {
#ifdef HANDLE
        _phDepositRateHelper = NULL;
#endif
        _Crate = safe_cast<CQuote^> (rate);
        _Crate->Lock();
        Handle<QuantLib::Quote>& _rate = static_cast<Handle<QuantLib::Quote>&> (_Crate->GetHandle ()); 
        _CiborIndex = safe_cast<CIborIndex^> (iborIndex);
        _CiborIndex->Lock();
        boost::shared_ptr<QuantLib::IborIndex>& _iborIndex = static_cast<boost::shared_ptr<QuantLib::IborIndex>&> (_CiborIndex->GetShared ()); 
        _ppDepositRateHelper = new boost::shared_ptr<QuantLib::DepositRateHelper> (new QuantLib::DepositRateHelper ( _rate,  _iborIndex ));
        SetRateHelper (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppDepositRateHelper));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Crate != nullptr) _Crate->Unlock();
        if (_CiborIndex != nullptr) _CiborIndex->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::CDepositRateHelper::CDepositRateHelper (Double rate, Cephei::QL::Times::IPeriod^ tenor, UInt32 fixingDays, Cephei::QL::Times::ICalendar^ calendar, QL::Times::BusinessDayConventionEnum convention, Boolean endOfMonth, Cephei::QL::Times::IDayCounter^ dayCounter) : CRateHelper(CDepositRateHelper::typeid)
{
    CPeriod^ _Ctenor;
    CCalendar^ _Ccalendar;
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phDepositRateHelper = NULL;
#endif
        QuantLib::Rate _rate = (QuantLib::Rate)ValueHelper::Convert (rate); //d
        _Ctenor = safe_cast<CPeriod^> (tenor);
        _Ctenor->Lock();
        QuantLib::Period& _tenor = static_cast<QuantLib::Period&> (_Ctenor->GetReference ()); 
        QuantLib::Natural _fixingDays = (QuantLib::Natural)ValueHelper::Convert (fixingDays); //d
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        QuantLib::BusinessDayConvention _convention = (QuantLib::BusinessDayConvention)convention ;
        bool _endOfMonth = (bool)ValueHelper::Convert (endOfMonth); //d
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        _ppDepositRateHelper = new boost::shared_ptr<QuantLib::DepositRateHelper> (new QuantLib::DepositRateHelper ( _rate,  _tenor,  _fixingDays,  _calendar,  _convention,  _endOfMonth,  _dayCounter ));
        SetRateHelper (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppDepositRateHelper));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ctenor != nullptr) _Ctenor->Unlock();
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::CDepositRateHelper::CDepositRateHelper (Cephei::QL::IQuote^ rate, Cephei::QL::Times::IPeriod^ tenor, UInt32 fixingDays, Cephei::QL::Times::ICalendar^ calendar, QL::Times::BusinessDayConventionEnum convention, Boolean endOfMonth, Cephei::QL::Times::IDayCounter^ dayCounter) : CRateHelper(CDepositRateHelper::typeid)
{
    CQuote^ _Crate;
    CPeriod^ _Ctenor;
    CCalendar^ _Ccalendar;
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phDepositRateHelper = NULL;
#endif
        _Crate = safe_cast<CQuote^> (rate);
        _Crate->Lock();
        Handle<QuantLib::Quote>& _rate = static_cast<Handle<QuantLib::Quote>&> (_Crate->GetHandle ()); 
        _Ctenor = safe_cast<CPeriod^> (tenor);
        _Ctenor->Lock();
        QuantLib::Period& _tenor = static_cast<QuantLib::Period&> (_Ctenor->GetReference ()); 
        QuantLib::Natural _fixingDays = (QuantLib::Natural)ValueHelper::Convert (fixingDays); //d
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        QuantLib::BusinessDayConvention _convention = (QuantLib::BusinessDayConvention)convention ;
        bool _endOfMonth = (bool)ValueHelper::Convert (endOfMonth); //d
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        _ppDepositRateHelper = new boost::shared_ptr<QuantLib::DepositRateHelper> (new QuantLib::DepositRateHelper ( _rate,  _tenor,  _fixingDays,  _calendar,  _convention,  _endOfMonth,  _dayCounter ));
        SetRateHelper (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppDepositRateHelper));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Crate != nullptr) _Crate->Unlock();
        if (_Ctenor != nullptr) _Ctenor->Unlock();
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Termstructures::Yield::CDepositRateHelper::CDepositRateHelper (boost::shared_ptr<QuantLib::DepositRateHelper>& childNative, Object^ owner) : CRateHelper(CDepositRateHelper::typeid)
{
#ifdef HANDLE
	_phDepositRateHelper = NULL;
#endif
	_ppDepositRateHelper = &childNative;
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppDepositRateHelper));
}
Cephei::QL::Termstructures::Yield::CDepositRateHelper::CDepositRateHelper (QuantLib::DepositRateHelper& childNative, Object^ owner) : CRateHelper(CDepositRateHelper::typeid)
{
#ifdef HANDLE
	_phDepositRateHelper = NULL;
#endif
	_ppDepositRateHelper = new boost::shared_ptr<QuantLib::DepositRateHelper> (&childNative);
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppDepositRateHelper));
    _DepositRateHelperOwner = owner;
    _RateHelperOwner = owner;
}

Cephei::QL::Termstructures::Yield::CDepositRateHelper::CDepositRateHelper (CDepositRateHelper^ copy) : CRateHelper(CDepositRateHelper::typeid)
{
#ifdef HANDLE
	_phDepositRateHelper = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppDepositRateHelper = new boost::shared_ptr<QuantLib::DepositRateHelper> (copy->GetShared());
        _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppDepositRateHelper));
    }
}
Cephei::QL::Termstructures::Yield::CDepositRateHelper::CDepositRateHelper (PLATFORM::Type^ t) : CRateHelper(CDepositRateHelper::typeid)
{
#ifdef HANDLE
	_phDepositRateHelper = NULL;
#endif
	if (!t->IsSubclassOf(CDepositRateHelper::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Yield::CDepositRateHelper::CDepositRateHelper (QuantLib::Handle<QuantLib::DepositRateHelper>& childNative, Object^ owner)  : CRateHelper(CDepositRateHelper::typeid)
{
	_phDepositRateHelper = &childNative;
	_ppDepositRateHelper = &static_cast<boost::shared_ptr<QuantLib::DepositRateHelper>>(childNative.currentLink());
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppDepositRateHelper));
    _DepositRateHelperOwner = owner;
}
Cephei::QL::Termstructures::Yield::CDepositRateHelper::CDepositRateHelper (QuantLib::Handle<QuantLib::DepositRateHelper> childNative)  : CRateHelper(CDepositRateHelper::typeid)
{
	_phDepositRateHelper = &childNative;
	_ppDepositRateHelper = &static_cast<boost::shared_ptr<QuantLib::DepositRateHelper>>(childNative.currentLink());
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppDepositRateHelper));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Yield::CDepositRateHelper::CDepositRateHelper (QuantLib::DepositRateHelper childNative)  : CRateHelper(CDepositRateHelper::typeid)
{
#ifdef HANDLE
	_phDepositRateHelper = NULL;
#endif
	_ppDepositRateHelper = new boost::shared_ptr<QuantLib::DepositRateHelper> (new QuantLib::DepositRateHelper (childNative));
    _ppRateHelper = new boost::shared_ptr<QuantLib::RateHelper> (boost::dynamic_pointer_cast<QuantLib::RateHelper> (*_ppDepositRateHelper));
}
#endif

Cephei::QL::Termstructures::Yield::CDepositRateHelper::~CDepositRateHelper ()
{
    if (_ppDepositRateHelper != NULL)
    {
	    delete _ppDepositRateHelper;
        _ppDepositRateHelper = NULL;
    }
}
Cephei::QL::Termstructures::Yield::CDepositRateHelper::!CDepositRateHelper ()
{
    if (_ppDepositRateHelper != NULL)
    {
	    delete _ppDepositRateHelper;
    }
}
QuantLib::DepositRateHelper& Cephei::QL::Termstructures::Yield::CDepositRateHelper::GetReference ()
{
    if (_ppDepositRateHelper == NULL) throw REFNEW NativeNullException ();
	return **_ppDepositRateHelper;
}
boost::shared_ptr<QuantLib::DepositRateHelper>& Cephei::QL::Termstructures::Yield::CDepositRateHelper::GetShared ()
{
    if (_ppDepositRateHelper == NULL) throw REFNEW NativeNullException ();
	return *_ppDepositRateHelper;
}
QuantLib::DepositRateHelper* Cephei::QL::Termstructures::Yield::CDepositRateHelper::GetPointer ()
{
    if (_ppDepositRateHelper == NULL) throw REFNEW NativeNullException ();
	return &**_ppDepositRateHelper;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::DepositRateHelper>& Cephei::QL::Termstructures::Yield::CDepositRateHelper::GetHandle ()
{
	if (_phDepositRateHelper == NULL)
	{
		_phDepositRateHelper = new Handle<QuantLib::DepositRateHelper> (*_ppDepositRateHelper);
	}
	return *_phDepositRateHelper;
}
#endif
bool Cephei::QL::Termstructures::Yield::CDepositRateHelper::HasNative () 
{
	return (_ppDepositRateHelper != NULL);
}

Double Cephei::QL::Termstructures::Yield::CDepositRateHelper::ImpliedQuote::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppDepositRateHelper)->impliedQuote ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Yield::IDepositRateHelper^ Cephei::QL::Termstructures::Yield::CDepositRateHelper::SetTermStructure (Cephei::QL::Termstructures::IYieldTermStructure^ t)
{
    CYieldTermStructure^ _Ct;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Ct = safe_cast<CYieldTermStructure^> (t);
        _Ct->Lock();
        QuantLib::YieldTermStructure* _t = static_cast<QuantLib::YieldTermStructure*> (_Ct->GetPointer ()); 
    	(*_ppDepositRateHelper)->setTermStructure ( _t );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ct != nullptr) _Ct->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Yield::IDepositRateHelper^ Cephei::QL::Termstructures::Yield::CDepositRateHelper_Factory::Create (Double rate, Cephei::QL::Indexes::IIborIndex^ iborIndex)
{
    return REFNEW CDepositRateHelper ( rate,  iborIndex);
}
Cephei::QL::Termstructures::Yield::IDepositRateHelper^ Cephei::QL::Termstructures::Yield::CDepositRateHelper_Factory::Create (Cephei::QL::IQuote^ rate, Cephei::QL::Indexes::IIborIndex^ iborIndex)
{
    return REFNEW CDepositRateHelper ( rate,  iborIndex);
}
Cephei::QL::Termstructures::Yield::IDepositRateHelper^ Cephei::QL::Termstructures::Yield::CDepositRateHelper_Factory::Create (Double rate, Cephei::QL::Times::IPeriod^ tenor, UInt32 fixingDays, Cephei::QL::Times::ICalendar^ calendar, QL::Times::BusinessDayConventionEnum convention, Boolean endOfMonth, Cephei::QL::Times::IDayCounter^ dayCounter)
{
    return REFNEW CDepositRateHelper ( rate,  tenor,  fixingDays,  calendar,  convention,  endOfMonth,  dayCounter);
}
Cephei::QL::Termstructures::Yield::IDepositRateHelper^ Cephei::QL::Termstructures::Yield::CDepositRateHelper_Factory::Create (Cephei::QL::IQuote^ rate, Cephei::QL::Times::IPeriod^ tenor, UInt32 fixingDays, Cephei::QL::Times::ICalendar^ calendar, QL::Times::BusinessDayConventionEnum convention, Boolean endOfMonth, Cephei::QL::Times::IDayCounter^ dayCounter)
{
    return REFNEW CDepositRateHelper ( rate,  tenor,  fixingDays,  calendar,  convention,  endOfMonth,  dayCounter);
}
